<?PHP
  class Language{
  
    public $languagePacks = null;
    public $language      = null;
    public $root          = "";
    
    public function __construct($token = null){
      if($token == null){
        $token = Settings::getInstance()->get("language");
      }
      $this->language = $token;
    }
    
    public function getString($token){
      $packageName = $this->getLanguagePackName($token);
      if(!$this->languagePacks[$packageName]){
        $this->openLanguagePack($packageName);
      }
      if($this->languagePacks[$packageName]){
        $res = $this->languagePacks[$packageName]->query("SELECT text FROM language WHERE token = '".$token."'");
        if($res){
          return $res->fetchSingle();
        }
      }
      return $token;
    }
    
    private function openLanguagePack($package){
      $create = false;
      if(!file_exists($this->getLanguagePackUrl($package))){
        $create = true;
      }
      $this->languagePacks[$package] = new SQLiteDatabase($this->getLanguagePackUrl($package),0666);
      if($create){
        $this->languagePacks[$package]->queryExec('CREATE TABLE language (token nvarchar(50), text nvarchar(4000), PRIMARY KEY (token));');
      }
    }
    
    private function getLanguagePackUrl($package){
      $namespaces = explode("_",strtolower($package));
      if(sizeOf($namespaces) == 2){
        if($namespaces[0] == "plugin"){
          return $this->root."system/plugins/".$namespaces[1]."/language/".$this->language.".db";
        }
        else if($namespaces[0] == "skin"){
          return $this->root."system/skins/".$namespaces[1]."/language/".$this->language.".db";
        }
      }
      return $this->root."system/language/".$this->language.".db";
    }
    
    private function getLanguagePackName($token){
      $namespaces = explode("_",strtolower($token));
      if(sizeOf($namespaces) == 3){
        if($namespaces[0] == "plugin"){
          return "plugin_".$namespaces[1];
        }
        else if($namespaces[0] == "skin"){
          return "skin_".$namespaces[1];
        }
      }
      return "global";
    }
    
    public function addTranslation($token,$text){
      $token = $GLOBALS['db']->EscapeString($token);
      $text = $GLOBALS['db']->EscapeString($text);
      $packageName = $this->getLanguagePackName($token);
      if(!$this->languagePacks[$packageName]){
        $this->openLanguagePack($packageName);
      }
      if($this->languagePacks[$packageName]){
        $this->languagePacks[$packageName]->queryExec("INSERT INTO language (token,text) VALUES ('".$token."','".$text."')");
      }
    }

    public function deleteTranslation($token){
      $token = $GLOBALS['db']->EscapeString($token);
      $packageName = $this->getLanguagePackName($token);
      if(!$this->languagePacks[$packageName]){
        $this->openLanguagePack($packageName);
      }
      if($this->languagePacks[$packageName]){
        $this->languagePacks[$packageName]->queryExec("DELETE FROM language WHERE token = '".$token."'");
      }
    }
    
    public function updateTranslation($token,$text){
      $this->deleteTranslation($token);
      $this->addTranslation($token,$text);
    }

    public function replaceLanguageTokensByObject($obj){
      if($obj){
        foreach($obj as $key => $value) {
          preg_match_all("/{LANG:(.+)}/", $value, $tokens, PREG_SET_ORDER);
          foreach($tokens as $token){
            $translation = $GLOBALS['language']->getString($token[1]);
            $obj->{$key} = str_ireplace($token[0],$translation,$obj->{$key});
          }
        }
      }
      return $obj;
    }
  }
?>